Introduction to Python

What is Python?

Python is a widely used high-level programming language for general-purpose programming, created by Guido Van Rossum and first released in 1991. An interpreted language, Python has a design philosophy which emphasizes code readability (notably using whitespace indentation to delimit code blocks rather than curly brackets or keywords), and a syntax which allows programmers to express concepts in fewer lines of code than might be used in languages such as C++ or Java. The language provides constructs intended to enable writing clear programs on both a small and large scale.

Prerequisites

We assume that you have:

  • Basic understanding of what computer does and what computer programs do
  • Knowledge of C Language
  • Knowledge of Object Oriented Concepts like Objects,Classes,Inheritance,Polymorphism, etc…
  • Knowledge of any Object Oriented Language like C++,Java or C#

These Software must be installed to follow the Language tutorial.

We will be using several libraries throughout the tutorial. They can be installed with pip (or pip3) as pip install <library-name>. Following is a list of libraries that has to be installed to follow the tutorial.

  • matplotlib (For visualizing data sets)
  • numpy (For faster array operations)
  • networkx (Provides Graph Data Structure) - OpenAnalysis (An open source package to analyse and visualise algorithms and data structures)

WARNING

The Python executable is python or python3 depending on your type of installation. Use --version flag with python executable to determine the version of Python installed on your system

Your First Program

As a tradition, we start with a program to display Hello World on the Console Screen (the stdout)

Open the Interative Python Shell by typing ipython(or ipython3) from the terminal. If everything goes well, you will get a prompt where you can enter statements and see the effect of it. Now enter the following statement to get started

In [1]:
print("Hello World")
Hello World

Congrats! You have successfully executed your first statement in Python. In fact there are many ways to execute python statements. Interactive Console is one of them. You can also pack the statements into single file, whose name ends with an extension .py, and call python (or python3) to execute them. We will also check this method to execute Python statements. Save the contents of below cell into a file named first.py.

Run this command to execute the file

python3 first.py  # Or python
In [1]:
print("Hello World")
print("Hi from Python File")
Hello World
Hi from Python File

Note: The usage of print() function is

print(list_of_values[,sep,end])
  • sep is the seperator string to be printed in between values in list_of_values
  • end is the terminating string that has to be printed after list_of_values

Example:

In [2]:
print('a','b','c',sep=':',end=',')
print('e','f','g',sep='.')
a:b:c,e.f.g